package com.lagou.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

@Activate(group = {CommonConstants.CONSUMER})
public class TPMonitorFilter implements Filter {
    public static ThreadLocal<List<Long>> methodATime = new ThreadLocal<>();
    public static ThreadLocal<List<Long>> methodBTime = new ThreadLocal<>();
    public static ThreadLocal<List<Long>> methodCTime = new ThreadLocal<>();

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        long startTime = System.currentTimeMillis();
        try{
            return invoker.invoke(invocation);
        }finally {
            if (!SingleThread.flag){
                SingleThread.getInstance().start();//单线程每5秒打印TP90TP99
            }
            long current = System.currentTimeMillis();
            long spendTime = current - startTime;//耗时
            if("methodA".equals(invocation.getMethodName())){
                if (methodATime.get() == null){
                    List<Long> list  = new ArrayList<>();
                    list.add(spendTime);
                    methodATime.set(list);
                }else{
                    if (methodATime.get().size()>1000){
                        List<Long> longs = methodATime.get();
                        List<Long> list = longs.subList(500, 1000);
                        methodATime.set(list);
                    }
                    methodATime.get().add(spendTime);
                }
                List<Long> list  = new ArrayList<>();
                list.addAll(methodATime.get());
                SingleThread.getInstance().setMethodAList(list);
            }else if("methodB".equals(invocation.getMethodName())){
                if (methodBTime.get() == null){
                    List<Long> list  = new ArrayList<>();
                    list.add(spendTime);
                    methodBTime.set(list);
                }else{
                    if (methodBTime.get().size()>1000){
                        List<Long> longs = methodBTime.get();
                        List<Long> list = longs.subList(500, 1000);
                        methodBTime.set(list);
                    }
                    methodBTime.get().add(spendTime);
                }
                List<Long> list  = new ArrayList<>();
                list.addAll(methodBTime.get());
                SingleThread.getInstance().setMethodBList(list);
            }else if("methodC".equals(invocation.getMethodName())){
                if (methodCTime.get() == null){
                    List<Long> list  = new ArrayList<>();
                    list.add(spendTime);
                    methodCTime.set(list);
                }else{
                    if (methodCTime.get().size()>1000){
                        List<Long> longs = methodCTime.get();
                        List<Long> list = longs.subList(500, 1000);
                        methodCTime.set(list);
                    }
                    methodCTime.get().add(spendTime);
                }
                List<Long> list  = new ArrayList<>();
                list.add(spendTime);
                SingleThread.getInstance().setMethodCList(list);
            }
        }
    }
}
